from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
from Crypto.Random import get_random_bytes

from Crypto.Protocol.KDF import PBKDF2
from Crypto.Hash import SHA512
from Crypto.Random import get_random_bytes

import configparser
import pathlib
import os
import sys
import wx
import pyautogui

from pymysql import NULL
root=pathlib.Path(os.path.dirname(os.path.realpath(sys.argv[0])))
conf = configparser.ConfigParser(allow_no_value=True)
try:
    conf.read(str(root/"config.ini"), encoding="utf-8")
except :
    try :
        print("config read for GBK")
        conf.read(str(root/"config.ini"), encoding="GBK")
    except:
        print("配置文件读取错误")

from secret import *
salt = get_random_bytes(16)
keys = PBKDF2(password, salt, 32, count=100000, hmac_hash_module=SHA512)
key1 = keys[:16]
key2 = keys[16:]

print(keys)
ciphertext=ak.encode()
cipher = AES.new(keys, AES.MODE_GCM,salt)
ciphertext = salt+cipher.encrypt(ak.encode())
print(b2a_hex(ciphertext).decode("utf-8"))
conf.set("config","ak",b2a_hex(ciphertext).decode("utf-8"))
conf.write(open(str(root/"config.ini"), "w"))

cipher = AES.new(keys, AES.MODE_GCM,salt)
ciphertext = salt+cipher.encrypt(sk.encode())
conf.set("config","sk",b2a_hex(ciphertext).decode("utf-8"))
print(b2a_hex(ciphertext).decode("utf-8"))
conf.write(open(str(root/"config.ini"), "w"))

ak=a2b_hex(conf.get("config","sk"))

def getCryptoConfig(key,password):
    data=a2b_hex(conf.get("config",key))
    vi=data[:16]
    data=data[16:]
    keys = PBKDF2(password, vi, 32, count=100000, hmac_hash_module=SHA512)
    cipher = AES.new(keys, AES.MODE_GCM,vi)
    data=cipher.decrypt(data).decode()
    print(data)
    return data
app = wx.App()
app.MainLoop()
wx.MessageBox("已更新！", " " ,wx.OK | wx.ICON_INFORMATION) 
